# 作者：石吴震
# 2024年12月31日12时18分57秒
# 474160957@qq.com

  #1、练习封装案例
class plant():
    def __init__(self, name, blood):
        self.name = name
        self.blood = blood
        self.free_blood = blood

    def __str__(self):
        if self.free_blood<=0:
            return f'{self.name} 被僵尸吃掉了'
        return f'{self.name} 的剩余血量为 {self.free_blood}'

    def fire(self, zombie):
        if zombie.free_blood<=0:
            return
        print(f'{self.name} 向 {zombie.name} 发射一颗豌豆子弹')
        zombie.free_blood -= 3


class zombie():
    def __init__(self, name, blood):
        self.name = name
        self.blood = blood
        self.free_blood = blood

    def __str__(self):
        if self.free_blood<=0:
            return f'{self.name} 被植物击倒了'
        return f'{self.name} 的剩余血量为 {self.free_blood}'

    def eat(self, plant):
        if plant.free_blood<=0:
            return
        print(f'{self.name} 吃了 {plant.name}一口')
        plant.free_blood -= 10



if __name__ == '__main__':
    wandousheshou=plant('豌豆射手', 40)
    jiangshi=zombie('僵尸', 30)
    wandousheshou.fire(jiangshi)
    jiangshi.eat(wandousheshou)
    print(wandousheshou)
    print(jiangshi)

 #2、练习私有属性和私有方法

class the_rich_man():
    def __init__(self, name):
        self.name = name
        self.__money = 5

    def __secret_money(self):
        print(f'小偷打开【{self.name}】的钱包, 发现里面有{self.__money}块钱,小偷骂骂咧咧地走开了')

    def thief(self):
        self.__secret_money()

if __name__ == '__main__':
    rich_man = the_rich_man('钻石王老五')
    rich_man.thief()

 #3、练习单继承，多重继承案例

class Parent():
    def __init__(self,name):
        self.name=name

class Son1(Parent):
    def __init__(self,age,*args):
        self.age=age
        super().__init__(*args)

class Son2(Parent):
    def __init__(self, score,*args):
        self.score=score
        super().__init__(*args)
class Grandson(Son1, Son2):
    def __init__(self, height,*args):
        self.height=height
        super().__init__(*args)  # 调用父类的构造函数,该地址还是Grandson的地址


if __name__ == '__main__':
    xia0ming=Grandson(180,10,90,'小明' )
    print(xia0ming.height)
    print(xia0ming.age)
    print(xia0ming.score)
    print(xia0ming.name)
    print(Grandson.__mro__)

 #4、实现单例模式

class Singleton(object):
    instance = None
    def __new__(cls, *args, **kwargs):
        if cls.instance==None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self, name):
        self.name=name

if __name__ == '__main__':
    s1 = Singleton('两只老虎')
    print(id(s1))
    s2 = Singleton('一只老虎')
    print(id(s2))
    print(s1.name)
    print(s2.name)

  #5、通过try进行异常捕捉，确保输入的内容一定是一个整型数，然后判断该整型数是否是对称数，12321就是对称数，
  # 123321也是对称数，否则就打印不是，非整型抛异常，不是对称数抛异常

def Abnormal_capture():
    num=int(input('请输入一个整数：'))
    a=str(num)
    b=a[::-1]
    if a==b:
        return f'{num}是对称数'
    raise Exception(f'{num}不是对称数')


if __name__ == '__main__':
        try:
            print(Abnormal_capture())
        except ValueError:
            print('您输入的不是一个整数')
        except Exception as e:
            print(e)